<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Services extends CI_Controller {
    /* PAGES */

    /**
     * Constructeur de la class Services.
     */
    public function __construct() {
        parent::__construct();

        $this->acl->setModule('services');
    }

    /**
     * Page d'administration des services.
     */
    public function admin() {
        $this->acl->can('manage');

        $crud = new grocery_CRUD();

        $crud->set_table('S_BUSINESS_DATA.V_SERVICE_SRV')
                ->set_subject('un service')

                // Champs.
                ->columns('SRV_NAME')
                ->fields('SRV_NAME')
                ->required_fields('SRV_NAME')

                // Affichage.
                ->display_as('SRV_NAME', 'Nom')
        ;
        $crud->order_by('SRV_NAME', 'ASC');

        // Callbacks
        $crud->callback_delete(array($this, 'callbackServiceDelete'));
        $crud->callback_insert(array($this, 'callbackServiceInsert'));
        $crud->callback_update(array($this, 'callbackServiceUpdate'));

        $this->db = $this->load->database('DB_PLASTPROD', true);
        $output = $crud->render();

        $css = $crud->get_css_files();
        $js = $crud->get_js_files();

        $this->layout
                ->setTitle('Services')
                ->setCss($css)
                ->setJs($js)
                ->view('services/admin', $output)
                ->display()
        ;
    }

    /* CALLBACKS */

    /**
     * Callback de suppression d'un service.
     * 
     * @param   int     $id
     * @return  boolean
     */
    public function callbackServiceDelete($id) {
        if ($this->acl->can('manage', false)) {
            $datas = array(
                $id
            );

            $result = callProc('DB_PLASTPROD', 'S_BUSINESS_DATA', 'P_D_SERVICE_00', $datas);

            if ('SUCCESS' === $result->RESULT)
                return true;
        }
        return false;
    }

    /**
     * Callback de création d'un service.
     * 
     * @param   array   $post
     * @return  boolean
     */
    public function callbackServiceInsert($post) {
        if ($this->acl->can('manage', false)) {
            $datas = array(
                '\'' . pg_escape_string($post['SRV_NAME']) . '\''
            );

            $result = callProc('DB_PLASTPROD', 'S_BUSINESS_DATA', 'P_I_SERVICE_00', $datas);

            if ('SUCCESS' === $result->RESULT)
                return true;
        }
        return false;
    }

    /**
     * Callback de mise à jour d'un service.
     * 
     * @param   array   $post
     * @param   int     $id
     * @return  boolean
     */
    public function callbackServiceUpdate($post, $id) {
        if ($this->acl->can('manage', false)) {
            $datas = array(
                $id,
                '\'' . pg_escape_string($post['SRV_NAME']) . '\''
            );

            $result = callProc('DB_PLASTPROD', 'S_BUSINESS_DATA', 'P_U_SERVICE_00', $datas);

            if ('SUCCESS' === $result->RESULT)
                return true;
        }
        return false;
    }

}